# @Time : 2023/1/5
# @Author : SDAJAVA

import urllib.request

url = 'https://movie.douban.com/j/chart/top_list?type=13&interval_id=100:90&action=&start=0&limit=20'

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}

request = urllib.request.Request(url=url, headers=headers)

response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
print(content)
# open方法默认情况下是GBK编码，如果要保存汉字，就需要在open方法制定编码格式为utf-8
# encoding = 'utf-8'
fp = open('douban.json', 'w', encoding='utf-8')
fp.write(content)

# 另一种方法
# with open('douban.json', 'w', encoding='utf-8') as fp:
#     fp.write(content)
